package frame

import "panel"

type FramePanel struct {
    panel.Common
    inside panel.Type
    header panel.Type
    scroll *panel.ScrollablePanel
}

type Null struct {
	panel.Common
}

func NewFrame(d panel.Doer, inside panel.Type, scroll *panel.ScrollablePanel, header panel.Type) panel.Type {
    if header == nil { header = &Null{} }
    f := &FramePanel{inside: inside, header: header, scroll: scroll}
    if scroll != nil { scroll.Parent(f) }
    inside.Parent(f)
    header.Parent(f)
    return f
}

func (f *FramePanel) FocusForClick(line, col int) panel.Type {
    if 0 < line && line < f.Lines - 1 && 0 < col && col < f.Cols - 1 { 
        return f.inside.FocusForClick(line - 1, col - 1) 
    }
    if line == 0 && 0 < col && col < f.Cols - 2 {
        return f.header.FocusForClick(0, col - 1)
    }
    return f 
}

func (f *FramePanel) Refresh() {
    f.inside.Refresh()
    f.header.Refresh()
    lines, cols := f.Common.Lines, f.Common.Cols
    _, _ = lines, cols
    f.Box( 0, 0, lines, cols )
    // RESTORE for col := 0; col < cols; col += 1 { f.Put(lines-1, col, curses.ACS_HLINE) }
    // RESTORE for line := 0; line < lines; line += 1 { f.Put(line, 0, curses.ACS_VLINE) }
    if f.scroll == nil {
        for line := 0; line < lines; line += 1 { 
            // RESTORE f.Put(line, cols-1, curses.ACS_VLINE) 
        }
    } else {
        f.scroll.Refresh()
    }
    // RESTORE f.Put(0, 0, curses.ACS_ULCORNER)
    // RESTORE f.Put(0, cols-1, curses.ACS_URCORNER)
    // RESTORE f.Put(lines-1, 0, curses.ACS_LLCORNER)
    // RESTORE f.Put(lines-1, cols-1, curses.ACS_LRCORNER)
}

func (f *FramePanel) Cursor(line, col int) {
    f.Common.Parental.Cursor(line, col)
}

func (f *FramePanel) Key(rune int) panel.Type {
    return nil
}

func (f *FramePanel) Resize(lines, cols int, lineOffset, colOffset int) {
    f.Common.Resize(lines, cols, lineOffset, colOffset)
    f.inside.Resize(lines - 2, cols - 2, 1, 1)
    f.header.Resize(1, cols - 2, 0, 1)
    if f.scroll != nil { f.scroll.Resize(lines - 2, 1, lineOffset + 1, cols - colOffset - 1) }
}
